package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.DataGridView;
import com.southwind.common.PageModel;
import com.southwind.common.ResultObj;
import com.southwind.entity.AdEffect;
import com.southwind.service.AdEffectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/trainPlan")
public class AdEffectController {

    @Autowired
    private AdEffectService trainPlanService;
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response){
        trainPlanService.exportExcel(response);
    }
    @RequestMapping("/importExcel")
    public ResultObj importExcel(MultipartFile file){
        trainPlanService.importExcel(file);
        return ResultObj.COMPLETE_SUCCESS;
    }

    @RequestMapping("/loadAlltrainPlan")
    public DataGridView loadAlltrainPlan(PageModel pageModel, String sortField, String sortOrder) {
    // 初始化分页对象
    Page<AdEffect> page = new Page<>(pageModel.getPage(), pageModel.getLimit());

    // 构建查询条件
    QueryWrapper<AdEffect> wrapper = new QueryWrapper<>();

    // 判断是否需要排序
    if (sortField != null && !sortField.isEmpty() && sortOrder != null && !sortOrder.isEmpty()) {
        if ("asc".equalsIgnoreCase(sortOrder)) {
            wrapper.orderByAsc(sortField);
        } else {
            wrapper.orderByDesc(sortField);
        }
    }

    // 执行分页查询
    IPage<AdEffect> resultPage = trainPlanService.page(page, wrapper);

    // 返回结果给前端
    return new DataGridView(resultPage.getTotal(), resultPage.getRecords());
}

    @GetMapping("/loadAdPlatforms")
public ResultObj<List<String>> loadAdPlatforms() {
    List<String> platforms = trainPlanService.listAdPlatforms(); // 查询 ad_platform 表中的 title 列
    return new ResultObj<>(platforms, 200, "查询成功");
}


    @RequestMapping("/addtrainPlan")
    public ResultObj addtrainPlan(AdEffect trainPlan){
        System.out.println("【接收到数据】： " + trainPlan);
        boolean save = this.trainPlanService.save(trainPlan);
        if(save) return ResultObj.ADD_SUCCESS;
        return ResultObj.ADD_ERROR;
    }

    @GetMapping("/loadAdNames")
public ResultObj<List<String>> loadAdNames() {
    List<String> adNames = trainPlanService.listAdNames(); // 查询 ad_info 表中的 adName 列
    return new ResultObj<>(adNames, 200, "查询成功");
}

    @RequestMapping("/updateTrainPlan")
    public ResultObj updateTrainPlan(AdEffect trainPlan){
        boolean updateById = this.trainPlanService.updateById(trainPlan);
        if(updateById) return ResultObj.UPDATE_SUCCESS;
        return ResultObj.UPDATE_ERROR;
    }

    @RequestMapping("/deleteTrainPlan")
    public ResultObj deleteTrainPlan(Integer id){
        boolean removeById = this.trainPlanService.removeById(id);
        if(removeById) return ResultObj.DELETE_SUCCESS;
        return ResultObj.DELETE_ERROR;
    }

    @RequestMapping("/batchDeleteTrainPlan")
    public ResultObj batchDeleteTrainPlan(Integer[] ids){
        List<Integer> idList = Arrays.asList(ids);
        boolean removeByIds = this.trainPlanService.removeByIds(idList);
        if(removeByIds) return ResultObj.DELETE_SUCCESS;
        return ResultObj.DELETE_ERROR;
    }

}

